package edu.guigu.appquickstart.recyclerview.ex1;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

import edu.guigu.appquickstart.R;
import edu.guigu.appquickstart.recyclerview.bean.UserBean;


/**
 * @author yuanc
 * @Description: (用一句话描述)
 * @date 2025/8/5 9:49
 */
public class UserBeanAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private ArrayList<UserBean> dataList;

    public UserBeanAdapter(Context context, ArrayList<UserBean> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    @Override
    public int getItemViewType(int position) {
        int viewType = position % 7 == 0 ? 1 : 0;
        return viewType;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        switch (mode) {
            case MODE_GRID:
                final View itemView1 = LayoutInflater.from(context).inflate(R.layout.item_grid_, parent, false);
                return new MyViewHolder(itemView1);
            case MODE_LIST:
            default:
                if (viewType == 0) {
                    final View itemView2 = LayoutInflater.from(context).inflate(R.layout.item_list_, parent, false);
                    return new MyViewHolder(itemView2);
                } else {
                    final View itemView2 = LayoutInflater.from(context).inflate(R.layout.item_list_2, parent, false);
                    return new MyViewHolder2(itemView2);
                }
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        UserBean userBean = dataList.get(position);
        //
        if (holder instanceof MyViewHolder) {
            MyViewHolder viewHolder = (MyViewHolder) holder;
            viewHolder.textView.setText(userBean.getLogin());
            Glide.with(context)
                    .load(userBean.getAvatar_url())
                    .placeholder(R.drawable.ic_loading)
                    .error(R.drawable.ic_image)
                    .override(60, 60)
                    .centerCrop()
                    .into(viewHolder.imageView);
        }
        //
        if (holder instanceof MyViewHolder2) {
            MyViewHolder2 viewHolder = (MyViewHolder2) holder;
            viewHolder.tv_title.setText(userBean.getLogin());
            Glide.with(context)
                    .load(userBean.getAvatar_url())
                    .placeholder(R.drawable.ic_loading)
                    .error(R.drawable.ic_image)
                    .centerCrop()
                    .into(viewHolder.iv_avatar);
        }
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView textView;
        ImageView imageView;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.textview);
            imageView = itemView.findViewById(R.id.imageview);
            // 事件监听(单击)
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onClickListener != null)
                        onClickListener.onClick(v, getAdapterPosition());
                }
            });
            // 事件监听(长按),上下文菜单时必要
            itemView.setClickable(true);
            itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    // 获取到当前长按的是谁
                    currentPosition = getAdapterPosition();
                    return false;
                }
            });
        }
    }

    class MyViewHolder2 extends RecyclerView.ViewHolder {
        ImageView iv_avatar;
        TextView tv_title;

        public MyViewHolder2(@NonNull View itemView) {
            super(itemView);
            iv_avatar = itemView.findViewById(R.id.iv_avatar);
            tv_title = itemView.findViewById(R.id.tv_title);
            // 事件监听(单击)
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onClickListener != null)
                        onClickListener.onClick(v,getAdapterPosition());
                }
            });
            // 事件监听(长按),上下文菜单时必要
            itemView.setClickable(true);
            itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    // 获取到当前长按的是谁
                    currentPosition = getAdapterPosition();
                    return false;
                }
            });
        }
    }

    private OnMyItemViewOnClickListener onClickListener;

    public void setOnMyItemViewOnClickListener(OnMyItemViewOnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    interface OnMyItemViewOnClickListener {
        void onClick(View view, int position);
    }

    private int mode = MODE_LIST;
    public static final int MODE_LIST = 0;
    public static final int MODE_GRID = 1;

    public void setMode(int mode) {
        this.mode = mode;
    }

    private int currentPosition = -1;

    public int getCurrentPosition() {
        return currentPosition;
    }
}
